<template>
	<el-container style="height: 50px;">
		<el-main>
			{{message}}
		</el-main>
	</el-container>
</template>

<script>
import db from "@/utils/db";

export default {
	name:"ssoLogin",
	data() {
		return {
			islogin: false,
			message:"正在登录中，请稍等。。。",
			sso:null,
			toUrl:'http://localhost:2800/#/e3ee8a3d01d8d6147afd0cb3f30176b1/messageCenter',
		}
	},
	watch: {},
	beforeCreate() {
		//进入登录页面，初始化一次数据库
		db.create();
		//清除应用数据缓存
		this.$cache.data.clearAll();
		this.$cache.tree.clearAll();
		this.$cache.tree.autoInit();
		this.$cache.dic.initBigData();

	},
	mounted() {
		// http://localhost:2800/#/ssoLogin?TO_URL=&SSO=1j3Qwhm8mImuQ0WRdz4iJb9V1z420gpzmXJ73FsJ87abHMoXQIDDHRgBAh4cIK%2BywDn9zCOiOMS8UN92Rx9ImSXr1C8wdegstVi%2FA15cvxfMRCZvT%2BG32%2FUwvLj%2F%2Fdd9M%2FwNU%2BIRAM%2FyXO921RqZb9bSKoJV9RiCRq%2FJiIdeJJynk0sICGSEjKHefSRVh%2BMYl6s3OT%2BpXiZAU5FvWV1r5Q%3D%3D
		this.login();
	},
	methods: {
		async login() {
			this.islogin = true
			let queryParams = this.$route.query;
			this.sso = queryParams.SSO;
			this.toUrl = queryParams.TO_URL;

			let loginName = null;
			let loginType = null;
			if(queryParams.LOGIN_NAME){
				loginName = queryParams.LOGIN_NAME;
				loginType = "temp";//临时登录，OA待办使用
			}

			if(this.sso){
				loginName = this.sso;
				loginType = "cas";
			}

			console.log(queryParams,this.sso)
			let data = {
				// loginName: "013679",
				// loginName: "1j3Qwhm8mImuQ0WRdz4iJb9V1z420gpzmXJ73FsJ87abHMoXQIDDHRgBAh4cIK%2BywDn9zCOiOMS8UN92Rx9ImSXr1C8wdegstVi%2FA15cvxfMRCZvT%2BG32%2FUwvLj%2F%2Fdd9M%2FwNU%2BIRAM%2FyXO921RqZb9bSKoJV9RiCRq%2FJiIdeJJynk0sICGSEjKHefSRVh%2BMYl6s3OT%2BpXiZAU5FvWV1r5Q%3D%3D",
				loginName: loginName,
				loginType: loginType,
			}
			let user = await this.$API.auth.login.post(data);
			if (!user.success) {
				this.islogin = false
				this.message  = user.msg;
				return false
			} else {
				//读取角色和角色对应的资源信息，组装成前端数据格式
				let valid = await this.$cache.login.assemble(user, false,this);
				if (!valid) {
					this.islogin = false;
				}
				//初始化字典信息
				this.$cache.dic.init();

				let dashboardGrid = [];
				dashboardGrid.push("welcome");
				dashboardGrid.push("var");
				dashboardGrid.push("time");
				dashboardGrid.push("progress");
				dashboardGrid.push("echarts");
				dashboardGrid.push("about");
				this.$TOOL.data.set("DASHBOARDGRID", dashboardGrid);

				if(this.toUrl){
					this.$router.replace({
						path: this.toUrl
					})
				}else{
					//进入首页
					this.$router.replace({
						path: '/'
					})
				}
			}
		},

	}
}
</script>
